package git.soulbgm;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;

import java.util.Scanner;

public class Application {

    /*public static void ntpdate() throws IOException {
        NTPUDPClient timeClient = new NTPUDPClient();
        String timeServerUrl = "ntp.ntsc.ac.cn";
        InetAddress timeServerAddress = InetAddress.getByName(timeServerUrl);
        TimeInfo timeInfo = timeClient.getTime(timeServerAddress);
        TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println("时间：" + dateFormat.format(timeStamp.getDate()));
    }*/

    private static void startTcpServer(int port) {
        TcpServer tcpServer = new TcpServer();
        tcpServer.initConfig(1, 1, new TcpServerHandler());
        ChannelFuture future = tcpServer.bind(port);
        System.out.println("start tcp server success");
    }

    private static void connect(String host, int port) {
        TcpClient client = TcpClient.getClient();
        client.start(new TcpClientHandle());
        ChannelFuture future = client.connect(host, port);
    }

    private final static String PARAM_SERVER = "server";
    private final static String PARAM_CLIENT = "client";
    private final static String COMMAND_NTPDATE = "ntpdate";

    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            throw new Exception("参数为空");
        }
        String ntpIp = "";
        int ntpPort = 0;
        if (PARAM_SERVER.equals(args[0]) && args.length == 2) {
            int ntpServerPort = Integer.parseInt(args[1]);
            startTcpServer(ntpServerPort);
        } else if (PARAM_CLIENT.equals(args[0]) && args.length == 3) {
            ntpIp = args[1];
            ntpPort = Integer.parseInt(args[2]);
            connect(ntpIp, ntpPort);
        } else {
            throw new Exception("参数有误");
        }

        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();
        while (true) {
            if (next.equals(COMMAND_NTPDATE)) {
                if (PARAM_SERVER.equals(args[0])) {
                    for (Channel channel : TcpServerHandler.CLIENT_LIST) {
                        ChannelUtil.tcpSendMsg(channel, NtpdateUtil.getNtpResponsePacket(System.currentTimeMillis(), NtpdateUtil.getNtpRequestPacket()));
                    }
                } else {
                    ChannelUtil.tcpSendMsg(TcpClient.get(ntpIp, ntpPort).channel(), NtpdateUtil.getNtpRequestPacket());
                }
            }
            next = scanner.next();
        }

    }

    /**
     * byte数组转16进制字符串
     *
     * @param data byte数组
     * @return 字符串
     */
    public static String bytesString16(byte[] data, String linkStr) {
        boolean isEmpty = linkStr.isEmpty();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            sb.append(String.format("%02X", data[i]));
            if (!isEmpty) {
                sb.append(linkStr);
            }
        }
        return sb.toString();
    }

}
